* Directories
// cd /Users/xiaosongw/Dropbox/Research/InformedSources/Replication/Analysis
// local datdir /Users/xiaosongw/Dropbox/Research/InformedSources/Replication/Build/Output/

cd /Users/byrned/Dropbox/Research/InformedSources/Replication/Analysis/
local datdir /Users/byrned/Dropbox/Research/InformedSources/Replication/Build/Output/

*-Figure 5: How Margins Change After Coles Exits the Informed Sources Platform -*

* Load data
use `datdir'is_mel_p_2005_2019, clear

* Sample period
keep if t>=td(1may2015) & t<=td(31dec2017)

* day of week month of year dummies
gen moy = month(t)
gen dow = dow(t)

* Coles off the platform
gen coles_off=cond(t>td(15mar2016),1,0)

* Cost changes
bysort id (t): gen dc=c-c[_n-1]
gen dc0=dc if st_cyc_day==1
replace dc0=0 if dc0==.

* create lagged cost changes
gen pos_dc=dc if dc>0 & ~missing(dc)
replace pos_dc=0 if dc<=0
gen neg_dc=dc-pos_dc
forval i=1/7 {
	bysort id (t): gen pos_dc`i'=pos_dc[_n-`i']
	bysort id (t): gen neg_dc`i'=neg_dc[_n-`i']
}

* normalize cycle length
drop st_cyc_day_norm
keep if st_cyc_len > 6
gen st_cyc_day_norm = 0 if st_cyc_day == 0
forval i = 1/10	{
	replace st_cyc_day_norm = `i' if (st_cyc_day >= ((st_cyc_len - 1) * (`i' - 1) / 10 + 1)) & 	///
								   (st_cyc_day <= ((st_cyc_len - 1) * `i' / 10 + 1))
}
* drop last cycle day 
replace st_cyc_day_norm = . if st_cyc_day == st_cyc_len - 1

* dummies for all Coles vs rivals, pre/post case, by cycle decile (paper)
local nday = 10
forval j=1/2{
	if(`j'==1){
		forval k=0/`nday'{
			gen pre_coles_`k'=(1-coles_off)*cond(bid==3 & st_cyc_day_norm==`k' & st_cyc_id>0,1,0)	/* Coles */
		}
		forval k=0/`nday'{
			gen post_coles_`k'=coles_off*cond(bid==3 & st_cyc_day_norm==`k' & st_cyc_id>0,1,0)	/* Coles */
		}
	}	
	if(`j'==2){
		forval k=0/`nday'{
			gen pre_rival_`k'=(1-coles_off)*cond(bid!=3 & st_cyc_day_norm==`k' & st_cyc_id>0,1,0)	/* Rivals */
		}	
		forval k=0/`nday'{
			gen post_rival_`k'=coles_off*cond(bid!=3 & st_cyc_day_norm==`k' & st_cyc_id>0,1,0)	/* Coles */
		}		
	}
}

* dummies for all retailers, pre/post case, by cycle decile (appendix) 
forval j=1/6{
	forval k=0/`nday'{
		gen pre_`j'_`k'=(1-coles_off)*cond(bid==`j' & st_cyc_day_norm==`k' & st_cyc_id>0,1,0)
	}	
	forval k=0/`nday'{
		gen post_`j'_`k'=coles_off*cond(bid==`j' & st_cyc_day_norm==`k' & st_cyc_id>0,1,0)
	}	
}
keep if bid>=1 & bid<=6


* save final data
sort id t
save Temp/regression_data, replace


* ----------------------- Margin Regression Coles vs Rivals -------------------*


* Load Data
use Temp/regression_data, clear

drop if t >= td(1mar2016) & t < td(1may2016)	/* cut out sample around the case */

* Melbourne (Coles vs Rivals)
reghdfe marg pre_coles_0-post_rival_10 pos_dc* neg_dc*, absorb(i.id i.dow i.moy) cluster(id t)

* Test for the change in undercutting speed for Coles
forval i=1/10{
	test (pre_coles_0-pre_coles_`i')=(post_coles_0-post_coles_`i')
}

* Estimates for plotting regression results
local nday = 10
tempname coef_plots
postfile `coef_plots' coef std lb ub bid cyc_day coles_off using Temp/mel_results_norm_dc, replace
forval j = 1/2 {
	if(`j'==1){
		forval i = 0/`nday' {
			qui lincom _cons +  pre_coles_`i'
			post `coef_plots' (r(estimate)) (r(se)) (r(lb)) (r(ub)) (`j') (`i') (0) 
			qui lincom _cons +  post_coles_`i'
			post `coef_plots' (r(estimate)) (r(se)) (r(lb)) (r(ub)) (`j') (`i') (1)
			qui lincom post_coles_`i' - pre_coles_`i'
			post `coef_plots' (r(estimate)) (r(se)) (r(lb)) (r(ub)) (`j') (`i') (-99)
		}		
	}
	if(`j'==2){
		forval i = 0/`nday' {
			qui lincom _cons +  pre_rival_`i'
			post `coef_plots' (r(estimate)) (r(se)) (r(lb)) (r(ub)) (`j') (`i') (0) 
			qui lincom _cons +  post_rival_`i'
			post `coef_plots' (r(estimate)) (r(se)) (r(lb)) (r(ub)) (`j') (`i') (1)
			qui lincom post_rival_`i' - pre_rival_`i'
			post `coef_plots' (r(estimate)) (r(se)) (r(lb)) (r(ub)) (`j') (`i') (-99)
		}		
	}	

}
postclose `coef_plots'


* ------------ Appendix Table with Margin Regression Coefficients -------------*

tempname coef_plots
postfile `coef_plots' coef std using Temp/app_baseline, replace
forval i=0/10{
		qui lincom pre_coles_`i'
		post `coef_plots'(r(estimate)) (r(se))
		qui lincom post_coles_`i' - pre_coles_`i'
		post `coef_plots' (r(estimate)) (r(se))
}
forval i=0/10{
		qui lincom pre_rival_`i'
		post `coef_plots'(r(estimate)) (r(se))
		qui lincom post_rival_`i' - pre_rival_`i'
		post `coef_plots' (r(estimate)) (r(se))
}
qui lincom pos_dc
post `coef_plots'(r(estimate)) (r(se))
forval i=1/7{
	qui lincom pos_dc`i'
	post `coef_plots'(r(estimate)) (r(se))
		
}
qui lincom neg_dc
post `coef_plots'(r(estimate)) (r(se))
forval i=1/7{
	qui lincom neg_dc`i'
	post `coef_plots'(r(estimate)) (r(se))
		
}	
qui lincom _cons
post `coef_plots'(r(estimate)) (r(se))	
postclose `coef_plots'

* Appendix table
use Temp/app_baseline, clear
gen name=""
forval i=0/10{
	local k1=(`i'*2)+1
	local k2=`k1'+1
	local k3=`k1'+22
	local k4=`k2'+22
	qui replace name="$\hat{\beta}^{coles}_{`i'}$" if _n==`k1'
	qui replace name="$\hat{\gamma}^{coles}_{`i'}$" if _n==`k2'
	qui replace name="$\hat{\beta}^{other}_{`i'}$" if _n==`k3'
	qui replace name="$\hat{\gamma}^{other}_{`i'}$" if _n==`k4'	
}
local k5=`k4'+1
forval i=0/7{
	qui replace name="$\hat{\delta}^{+}_{`i'}$" if _n==`k5'
	local k5=`k5'+1
}
forval i=0/7{
	qui replace name="$\hat{\delta}^{-}_{`i'}$" if _n==`k5'
	local k5=`k5'+1
}
qui replace name="$\hat{\alpha}_{0}$" if _n==_N

gen am1="&"
gen am2="& ("
gen am3=") \\"
qui replace am3=")" if _n==_N
tostring coef, gen(coef_str) format(%12.2f) force
tostring std, gen(std_str) format(%12.2f) force
order name am1 coef_str am2 std_str am3
keep name am1 coef_str am2 std_str am3
outsheet using Output/tabC8_baseline_estimates.tex, delim(" ") nonames noquote replace


*-----------------------------  Panel (a) Coles -------------------------------*

* Melbourne Coles vs Rivals
use Temp/mel_results_norm_dc, clear

keep if coles_off >= 0
replace cyc_day = cyc_day / 10

* Coles	
local y1=27.5
local y2=27.5
local x1=0.15
local x2=0.25
local xtext1=0.45
local ytext1=27.5	
local y3=10.5
local y4=7.5
local x3=0.96
local x4=0.96
local ytext2=12.5
local xtext2=0.95	
local panel1=0.035
local panel2=0.075

twoway 	(line coef cyc_day if coles_off==0&bid==1, lcolor(gs0*0.3) lpattern(solid) lwidth(medthick)) ///
		(line lb cyc_day if coles_off==0&bid==1, lcolor(gs0*0.3) lpattern(dash) lwidth(vthin)) ///
		(line ub cyc_day if coles_off==0&bid==1, lcolor(gs0*0.3) lpattern(dash) lwidth(vthin)) ///
		(line coef cyc_day if coles_off==1&bid==1, lcolor(gs0) mcolor(gs0) lpattern(solid) lwidth(medthick)) ///
		(line lb cyc_day if coles_off==1&bid==1, lcolor(gs0) mcolor(red) lpattern(dash) lwidth(vthin)) ///
		(line ub cyc_day if coles_off==1&bid==1, lcolor(gs0) mcolor(red) lpattern(dash) lwidth(vthin) ///
		xtitle("Fraction of the Cycle Completed", height(0)) ytitle("Station-Level Margin Estimate (cpl)") ///
		xlabel(0 `" "0" "Restoration" "' .5 "1/2" 1 `" "1" "Full" "Cycle" "',labsize(*0.8)) ylabel(0(5)31, grid gmin gmax) ///
		xscale(r(0, 1.01))	yscale(r(-1.5, 27.3)) ///
		plotregion(margin(zero) style(none) fcolor(white)) graphregion(color(white)) bgcolor(white) ///
		legend(ring(0) size(*0.7) position(7) symx(*0.5) region(lwidth(*0.05)) rows(2) order(1 2 4 5) ///
		label(1 "Coles {bf:ON}" "Informed Sources") ///
		label(4 "Coles {bf:OFF}" "Informed Sources") ///
		label(2 "95% CI") label(5 "95% CI"))) ///
		(pcarrowi `y2' `x2' `y1' `x1', color(black) lwidth(*1) barbsize(1) msize(2) ///
		text(`ytext1' `xtext1' "Restoration margins increase;" "Coles delays undercutting", justification(left) size(*.85))) ///
		(pcarrowi `y3' `x3' `y4' `x4', color(black) lwidth(*1) barbsize(1) msize(2) ///
		text(`ytext2' `xtext2' "Coles no" "longer cuts" "margins to 0", justification(center) size(*.85)) ///
		text(30.75 `panel1' "{bf:A}", justification(left) size(*2)) ///
		text(30.75 `panel2' "        Coles", justification(left) size(*1.25)))		
graph export Output/fig5a_margin_coles.pdf, as(pdf) replace


* Rivals pooled
local y1=22.5
local y2=22.5
local x1=0.15
local x2=0.25
local xtext1=0.43
local y3=12.5
local y4=12.5
local x3=0.55
local x4=0.4
local xtext2=0.71	
local panel1=0.035
local panel2=0.20

*----------------------  Panel (b) Other major retailers ----------------------*
		
twoway 	(line coef cyc_day if coles_off==0&bid==2, lcolor(gs0*0.3) lpattern(solid) lwidth(medthick)) ///
		(line lb cyc_day if coles_off==0&bid==2, lcolor(gs0*0.3) lpattern(dash) lwidth(vthin)) ///
		(line ub cyc_day if coles_off==0&bid==2, lcolor(gs0*0.3) lpattern(dash) lwidth(vthin)) ///
		(line coef cyc_day if coles_off==1&bid==2, lcolor(gs0) mcolor(black) lpattern(solid) lwidth(medthick)) ///
		(line lb cyc_day if coles_off==1&bid==2, lcolor(gs0) mcolor(black) lpattern(dash) lwidth(vthin)) ///
		(line ub cyc_day if coles_off==1&bid==2, lcolor(gs0) mcolor(black) lpattern(dash) lwidth(vthin) ///
		xtitle("Fraction of the Cycle Completed", height(0)) ytitle("Station-Level Margin Estimate (cpl)") ///
		xlabel(0 `" "0" "Restoration" "' .5 "1/2" 1 `" "1" "Full" "Cycle" "', labsize(*0.8)) ylabel(0(5)31, grid gmin gmax) ///
		xscale(r(0, 1.01))	yscale(r(-1.5, 27.3)) legend(off) ///
		plotregion(margin(zero) style(none) fcolor(white)) graphregion(color(white)) bgcolor(white)) ///
		(pcarrowi `y2' `x2' `y1' `x1', color(black) lwidth(*1) barbsize(1) msize(2) ///
		text(`y1' `xtext1' "Restoration margins increase", justification(left) size(*.85))) /// 
		(pcarrowi `y3' `x3' `y4' `x4', color(black) lwidth(*1) barbsize(1) msize(2) ///
		text(`y3' `xtext2' "Price undercutting softens", justification(left) size(*.85)) ///
		text(30.75 `panel1' "{bf:B}", justification(left) size(*2)) ///
		text(30.75 `panel2' "        Other major retailers", justification(left) size(*1.25))) 		
graph export Output/fig5b_margin_rival.pdf, as(pdf) replace



		
		
